// ImageRectTool.js
import { limitPointerInImage } from '../utils/imageUtils';

class PointTool {
    constructor(canvas) {
        this._canvas = canvas;
        this._point_coord = null;
        this._callback = null;
    }

    setImage(image) {
        this._image = image;
    }

    setCallback(callback) {
        this._callback = callback;
    }

    onMouseDown(event) {
        if (this._canvas.getActiveObject()) {
            return;
        }
        // console.log('PointTool::onMouseDown', event);
        let pointer = {
            x: event.absolutePointer.x,
            y: event.absolutePointer.y
        };
        limitPointerInImage(this._image, pointer);

        this._point_coord = { left: pointer.x, top: pointer.y };
        this.finish();
    }

    onMouseMove(event) {
    }

    handleKeyDown(event) {
        console.log('PointTool::handleKeyDown event = ', event);
        let key = event.key.toLowerCase();
        if (key == 'delete' || key == 'backspace') {
        }
    }

    // 完成当前编辑
    finish() {
        if (this._point_coord) {
            // 调用回调函数，新增一个rect标注结果
            this._callback({
                left: this._point_coord.left,
                top: this._point_coord.top
            });
        }
        this._point_coord = null;
    }

    // 取消当前编辑
    clear() {
        this._point_coord = null;
    }
}

export default PointTool;
